﻿using BadmintonBuddy.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BadmintonBuddy.Controllers
{
    public class ClubController : Controller
    {
        //
        // GET: /Club/
        [Authorize]
        [HttpGet]
        public ActionResult InsertClub()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult InsertClub(CreateClubViewModel newClubView)
        {
            if (ModelState.IsValid)
            {
                Club newClub = new Club();
                newClub.ClubName = newClubView.ClubName;
                newClub.Address = newClubView.Address;
                newClub.AdditionalInfo = newClubView.AdditionalInfo;
                newClub.Timings = newClubView.Timings;
                newClub.Fees = newClubView.Fees;
                newClub.SurfaceID = newClubView.Surface;
                newClub.CreatedDate = DateTime.Now;
                newClub.LastUpdated = DateTime.Now; // this will be same as CreatedDate for the new club created
                using (BadmintonBuddyEntities dbContext = new BadmintonBuddyEntities())
                {
                    newClub.City = Common.GetCity(newClubView.CityName.Trim(), newClubView.StateName.Trim(), newClubView.CountryName.Trim());

                    dbContext.Clubs.Add(newClub);
                    dbContext.SaveChanges();
                }
            }
            return View();  //Show users a confirmation page along with some users, details on the city where they have added data
            //also link to add anything more
        }

        //[HttpGet]
        //public ActionResult Details(int ClubID)
        //{
        //    Club club = null;
        //    using (BadmintonBuddyEntities dbContext = new BadmintonBuddyEntities())
        //    {
        //        IQueryable<Club> clubColl = from db_club in dbContext.Clubs
        //                                    where db_club.ClubID == ClubID
        //                                    select db_club;
        //        if (clubColl != null && clubColl.Count() > 0)
        //        {
        //            club = clubColl.First<Club>();
        //        }
        //    }
        //    return View(club);
        //}

        [HttpGet]
        public ActionResult Details(string ClubName)
        {
            Club club = null;
            using (BadmintonBuddyEntities dbContext = new BadmintonBuddyEntities())
            {
                IQueryable<Club> clubColl = from db_club in dbContext.Clubs
                                            where db_club.ClubName.ToLower() == ClubName.Trim().ToLower()
                                            select db_club;
                if (clubColl != null && clubColl.Count() > 0)
                {
                    club = clubColl.First<Club>();
                    ViewData["CityName"] = club.City.CityName;
                    ViewData["Surface"] = club.SurfaceType.Surface;
                }
                return View(club);
            }            
        }

        public ActionResult ClubPlayers(int clubID) 
        {
            using (BadmintonBuddyEntities dbContext = new BadmintonBuddyEntities())
            {
                //Add logic to get all the players of this club and send it to View


                IQueryable<UserProfile> clubPlayerColl = from db_user in dbContext.UserProfiles
                                                         join db_clubmember in dbContext.ClubMembers
                                                         on db_user.UserId equals db_clubmember.UserID
                                                         where db_clubmember.ClubID == clubID && db_clubmember.PlayStatus == 3  //PlayStatus = 3 is players looking for partner in this club
                                                         select db_user;
                return PartialView(clubPlayerColl.ToList());
            }

        }
        
        public ActionResult CityClubs(int CityID, int ClubID)
        {
            using (BadmintonBuddyEntities dbContext = new BadmintonBuddyEntities())
            {
                IQueryable<Club> clubCityColl = from db_club in dbContext.Clubs
                                                where db_club.CityID == CityID && db_club.ClubID != ClubID
                                                select db_club;
                return PartialView(clubCityColl.ToList());

            }            
        }

        public ActionResult AllClubsInCity(string cityname)
        {
            if (!String.IsNullOrEmpty(cityname))
            {
                using (BadmintonBuddyEntities dbContext = new BadmintonBuddyEntities())
                {
                    IQueryable<Club> cityClubsColl = from db_club in dbContext.Clubs
                                                     where db_club.City.CityName == cityname
                                                     select db_club;
                    return View(cityClubsColl.ToList());
                }
            }
            else
            {
                return View("Home/Index");
            }
            
        }
    }
}
